package cn.moukou.sbzz.controller;

import cn.moukou.sbzz.pojo.MetaClass;
import cn.moukou.sbzz.service.MetaClassService;
import cn.moukou.sbzz.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sbzz/api")
public class MetaClassController {

    @Autowired
    MetaClassService metaClassService;

    @PostMapping("/meta-class/add")
    public Resp<String> addMetaClass(@RequestParam("name") String name, @RequestParam("type") String type) {
        int cnt = metaClassService.addOneMetaClass(name, type);
        if(cnt > 0) {
            return Resp.ok("插入成功");
        } else {
            return Resp.fail("插入失败");
        }
    }

    @GetMapping("/meta-class/list")
    public Resp<List<MetaClass>> getMetaClassFormList(@RequestParam("type") String type) {
        List<MetaClass> metaClassList = metaClassService.getMetaClassListByType(type);
        return Resp.ok(metaClassList);
    }

    @PostMapping("/meta-class/update")
    public Resp<String> updateMetaClass(@RequestParam("id") String id, @RequestParam("name") String name) {
        int cnt = metaClassService.updateMetaClassName(id, name);
        if(cnt > 0) {
            return Resp.ok("修改成功");
        } else {
            return Resp.fail("修改失败");
        }
    }

    @PostMapping("/meta-class/delete")
    public Resp<String> deleteMetaClass(@RequestParam("id") String id) {
        int cnt = metaClassService.deleteMetaClass(id);
        if(cnt > 0) {
            return Resp.ok("删除成功");
        } else {
            return Resp.fail("删除失败");
        }
    }

}
